package com.seek.quartz;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.seek.service.InterService;

/**
 * 数据迁移处理
 * @author lushuangteng
 *
 */
public class DataBakQuartz {
	private static Logger logger = LoggerFactory.getLogger(DataBakQuartz.class);
	@Resource
	private InterService interService;

	public void run(){
		try{
			// 备份t_biz_flow:把5天前不存在于备份表中的数据统一写入到备份表，并把t_biz_flow中的5天前数据清空。
			StringBuffer sb = new StringBuffer("insert into t_biz_flow_all select * from t_biz_flow a where");
			sb.append(" a.create_time<date_sub(curdate(),interval 5 day) ")
			  .append(" and not exists (select 1 from t_biz_flow_all b where a.id=b.id)");
			interService.executeUpdate(sb.toString());
			sb = new StringBuffer("delete from t_biz_flow where create_time<date_sub(curdate(),interval 5 day)");
			
			// 备份t_sms_log，主库只保留近90天数据
			sb = new StringBuffer("insert into t_sms_log_all select * from t_sms_log a where ");
			sb.append(" a.create_time<date_sub(curdate(),interval 90 day) ")
			  .append(" and not exists (select 1 from t_sms_log_all b where a.id=b.id)");
			sb = new StringBuffer("delete from t_sms_log where create_time<date_sub(curdate(),interval 90 day)");
			
			// 备份t_company_agent_money
			sb = new StringBuffer("insert into t_company_agent_money_all select * from t_company_agent_money a where ");
			sb.append(" a.create_time<date_sub(curdate(),interval 90 day) ")
			  .append(" and not exists (select 1 from t_company_agent_money_all b where a.id=b.id)");
			sb = new StringBuffer("delete from t_company_agent_money where create_time<date_sub(curdate(),interval 90 day)");
		}catch(Exception e){
			logger.error(e.getMessage());
		}
	}
}
